package com.njworkorder.Service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.njworkorder.Entity.AdministrativePersonnel;
import com.njworkorder.Entity.AssignmentApprovalHistory;
import com.njworkorder.Entity.EngineeringQualityInfo;
import com.njworkorder.Service.AssignmentApprovalHistoryService;
import com.njworkorder.Mapper.AssignmentApprovalHistoryMapper;
import com.njworkorder.Utils.NoCode;
import com.njworkorder.VO.ApprovalHistoryListVo;
import com.njworkorder.VO.AssignmentProgressVos.CheckAcceptInfo;
import com.njworkorder.VO.AssignmentProgressVos.RepairInformationInfo;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.concurrent.CompletableFuture;

/**
* @author iron
* @description 针对表【assignment_approval_history】的数据库操作Service实现
* @createDate 2025-01-03 15:20:36
*/
@Service
public class AssignmentApprovalHistoryServiceImpl extends MPJBaseServiceImpl<AssignmentApprovalHistoryMapper, AssignmentApprovalHistory>
    implements AssignmentApprovalHistoryService{

    @Override
    public MPJLambdaWrapper<AssignmentApprovalHistory> getMPJWrapper() {
        return new MPJLambdaWrapper<AssignmentApprovalHistory>()
                .selectAll(AssignmentApprovalHistory.class)
                .leftJoin(AdministrativePersonnel.class,AdministrativePersonnel::getId,AssignmentApprovalHistory::getApId,ext ->
                        ext.selectAs(AdministrativePersonnel::getName, ApprovalHistoryListVo::getApName));
    }

    @Override
    public void saveApprovalHistory(int type,String approvalRemark,String apId,String assignmentId){
        String approvalDescription = NoCode.getApprovalDescription(type);

        LambdaQueryWrapper<AssignmentApprovalHistory> queryWrapper = new LambdaQueryWrapper<AssignmentApprovalHistory>()
                .eq(AssignmentApprovalHistory::getAssignmentId, assignmentId)
                .eq(AssignmentApprovalHistory::getApprovalDescription, approvalDescription);
        boolean exists = exists(queryWrapper);
        if (exists) {
            LambdaUpdateWrapper<AssignmentApprovalHistory> updateWrapper = new LambdaUpdateWrapper<AssignmentApprovalHistory>()
                    .eq(AssignmentApprovalHistory::getAssignmentId, assignmentId)
                    .eq(AssignmentApprovalHistory::getApprovalDescription, approvalDescription)
                    .set(AssignmentApprovalHistory::getApprovalRemark, approvalRemark)
                    .set(AssignmentApprovalHistory::getApId, apId)
                    .set(AssignmentApprovalHistory::getApprovalTime, new Date());
            update(updateWrapper);
            return;
        }
        AssignmentApprovalHistory approvalHistory = new AssignmentApprovalHistory();
        approvalHistory.setApprovalDescription(approvalDescription);
        approvalHistory.setApprovalRemark(approvalRemark);
        approvalHistory.setApId(apId);
        approvalHistory.setAssignmentId(assignmentId);
        approvalHistory.setApprovalTime(new Date());
        save(approvalHistory);
    }

    @Async
    @Override
    public CompletableFuture<CheckAcceptInfo> getCheckAcceptInfo(String assignmentId) {

        MPJLambdaWrapper<AssignmentApprovalHistory> wrapper = new MPJLambdaWrapper<AssignmentApprovalHistory>()
                .selectAs(AdministrativePersonnel::getName,CheckAcceptInfo::getCheckAcceptApName)
                .selectAs(AssignmentApprovalHistory::getApprovalTime,CheckAcceptInfo::getCheckAcceptTime)
                .selectAs(AssignmentApprovalHistory::getApprovalRemark,CheckAcceptInfo::getRemark)

                .leftJoin(AdministrativePersonnel.class,AdministrativePersonnel::getId,AssignmentApprovalHistory::getApId)

                .eq(AssignmentApprovalHistory::getAssignmentId,assignmentId)
                .eq(AssignmentApprovalHistory::getApprovalDescription,"验收初审");

        return CompletableFuture.completedFuture(selectJoinOne(CheckAcceptInfo.class, wrapper));
    }


}




